//
//  JYTMineViewController.m
//  JYT_IOS
//
//  Created by nxa-tankeke on 16/12/14.
//  Copyright © 2016年 nxa-yuanguoxiang. All rights reserved.
//

#import "JYTMineViewController.h"
#import "JYTLoginViewController.h"
#import "JYTMeHeadView.h"
#import "JYTTestViewController.h"
#import "JYTMine.h"
#import "JYTTableViewCell.h"
#import "User.h"
#import "JYTLearnTimeFooter.h"
#import "JYTMyInformationViewController.h"
#import "JYTLearCarRecordsViewController.h"
#import "JYTMyAppointViewController.h"
#import "JYTMyTrainerViewController.h"
#import "JYTMySettingViewController.h"
@interface JYTMineViewController ()<ProtocolInfoView,UITableViewDelegate,UITableViewDataSource,UIAlertViewDelegate>
@property (nonatomic, strong) JYTMeHeadView *header;
@property (nonatomic,strong)NSArray *dataArr;
@property (nonatomic,strong)UITableView *mytableView;
@end

@implementation JYTMineViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getHeadUrl:) name:@"headurl" object:nil];
    
    BOOL isUsedOffLine = [[JYTUserManager shared] isUsedOffLine];
   
    if (!isUsedOffLine) {
        NSDictionary *dict = @{};
        User *user =[[JYTUserManager shared] getUser];
        [self.header loginWithUserName:user.userName nick:[[JYTUserManager shared] getNikeName] headerUrl:[[JYTUserManager shared] getUserHeadUrl] learnTime:dict];
//        MyLog(@"头像地址%@",[[JYTUserManager shared] getNikeName]);
        [self.mytableView reloadData];
    }
}

-(void)getHeadUrl:(NSNotification *)noti{
    [[JYTUserManager shared] setUserHeadUrl:noti.object];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setNavgationBarView:@{@"pageTitle":@"我的", @"lftBtnTitle":@"no", @"rhtBtnTitle":@"no"}];
    
    JYTMeHeadView *header = [[JYTMeHeadView alloc] initWithFrame:CGRectMake(0, 64, CURRENT_SCREEN_WIDTH, 130)];
    header.delegate = self;
    self.header = header;
    [self.view addSubview:header];
    
    
    [self initData];
    [self initView];

}
- (void)initData{
    
    NSArray *titleImgArr = [NSArray arrayWithObjects:@"ic_home_zjx",@"ic_home_zjx", @"ic_home_zjx",@"ic_home_zjx",@"ic_home_zjx",@"ic_home_zjx",@"ic_home_zjx",nil];
    NSArray *contentArr = [NSArray arrayWithObjects:@"累计学时",@"学车记录",@"我的预约", @"我的教练",@"我的陪练",@"我的消息",@"设置", nil];
    
    
    NSMutableArray *temp = [NSMutableArray array];
    for (NSInteger i = 0; i < titleImgArr.count; i ++) {
        JYTMine *mine = [[JYTMine alloc]init];
        mine.content = contentArr[i];
        mine.imageName = titleImgArr[i];
        [temp addObject:mine];
    }
    
    NSArray *temp1 = [[NSArray alloc]initWithObjects:temp[0], nil];
    NSArray *temp2 = [[NSArray alloc]initWithObjects:temp[1],temp[2],temp[3],temp[4],temp[5],temp[6], nil];
 
    
    self.dataArr = [[NSArray alloc]initWithObjects:temp1,temp2, nil];
}

- (void)initView{
    
    self.mytableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 194, CURRENT_SCREEN_WIDTH, CURRENT_SCREEN_HEIGHT - 194 -44) style:UITableViewStyleGrouped];
    self.mytableView.backgroundColor = [UIColor whiteColor];
    [self.mytableView registerClass:[JYTTableViewCell class] forCellReuseIdentifier:@"mineCell"];
    self.mytableView.delegate = self;
    self.mytableView.dataSource = self;
    //self.mytableView.bounces = NO;
   
    [self.view addSubview:self.mytableView];
    
}
#pragma mark----UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    JYTTableViewCell *cell = [self.mytableView dequeueReusableCellWithIdentifier:@"mineCell"];
    cell.mine = [self.dataArr[indexPath.section] objectAtIndex:indexPath.row];
    if (indexPath.section==0) {
        cell.indcatorImg.hidden=YES;
    }
    return cell;
    
}
#pragma mark----UITableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else {
        return 6;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
   BOOL isOffLine = [[JYTUserManager shared] isUsedOffLine];
    
    if (section == 0) {
        
        if (!isOffLine) {
            return 95;
        } else {
            return 0.00001f;
        }
        
    } else {
         return 0.00001f;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.00001f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
  
    return 44 *(CURRENT_SCREEN_HEIGHT / 667);
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
//    if (section == 0) {
//        JYTLearnTimeFooter *foot = [[JYTLearnTimeFooter alloc]initWithFrame:CGRectMake(0, 0, CURRENT_SCREEN_WIDTH, 95)];
//        return foot;
//    } else{
//        return nil;
//    }
//    
    BOOL isOffLine = [[JYTUserManager shared] isUsedOffLine];
    if (section == 0) {
        
        if (!isOffLine) {
            JYTLearnTimeFooter *foot = [[JYTLearnTimeFooter alloc]initWithFrame:CGRectMake(0, 0, CURRENT_SCREEN_WIDTH, 95)];
            return foot;
        } else {
            return nil;
        }
    } else {
        return nil;
    }

}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //判断是否登陆
    BOOL isLogin = [[JYTUserManager shared] isUsedOffLine];
    if (indexPath.section==0) {
        MyLog(@"不许点击");
    }else{
        if (!isLogin) {//已登陆
            MyLog(@"%ld===%ld",(long)indexPath.section,(long)indexPath.row);
            switch (indexPath.row) {
                case 0://学车记录
                    [self pushViewControllerToContainer:[[JYTLearCarRecordsViewController alloc] init]];
                    break;
                case 1://我的预约
                    [self pushViewControllerToContainer:[[JYTMyAppointViewController alloc] init]];
                    break;
                case 2://我的教练
                    [self pushViewControllerToContainer:[[JYTMyTrainerViewController alloc] init]];
                    break;
                case 3://我的陪练
                {
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示：" message:@"当前功能正在开发中..." delegate:self cancelButtonTitle:nil otherButtonTitles:@"知道了！", nil];
                    [alert show];
                }
                    break;
                case 4://我的消息
                {
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示：" message:@"当前功能正在开发中..." delegate:self cancelButtonTitle:nil otherButtonTitles:@"知道了！", nil];
                    [alert show];
                }
                    break;
                case 5://设置
                    [self pushViewControllerToContainer:[[JYTMySettingViewController alloc] init]];
                    break;
                default:
                    break;
            
            }
        }else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示：" message:@"您当前尚未登陆，请先登录！" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"登陆", nil];
            [alert show];
        }
    }
}
#pragma mark----UIAlertViewDelegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex==0) {
        [alertView dismissWithClickedButtonIndex:buttonIndex animated:YES];
    }else{//跳转到登陆界面
        [self loginClicked];
    }
}
 - (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark ----ProtocolInfoView
- (void)clickInfoView
{
    JYTMyInformationViewController *vc = [[JYTMyInformationViewController alloc] init];
    [self pushViewControllerToContainer:vc];
}

- (void)loginClicked
{
    JYTLoginViewController *log = [[JYTLoginViewController alloc]init];
    [self pushViewControllerToContainer:log];
}
@end
